(ns ura.popup
  (:require
   [reagent.core :as r]
   [reagent.dom :as rd]

   [ura.style :refer [attr]]
   [ura.common :as uc :refer [log spy]]
   ))


(defn mask-div [id]
  [:div 
   (attr {:name (str "mask div " id) }
         :bgc=transparent :width=100% :height=100%
         :position=absolute :top=0px :left=0px
         {:on-click #(.remove (js/document.getElementById id))})
   ]
  )

(defn showPopup [ev nodes]
  (log "show popup " ev)
  (let [ node1 (js/document.createElement "div")
        id (str "popup-" (rand-int 1000000))
        selectBox ev.target.parentNode.parentNode.parentNode
        [posx posy] [selectBox.offsetLeft (+ selectBox.offsetTop  selectBox.offsetHeight)]
        [boxw boxh] [selectBox.clientWidth selectBox.clientHeight]
        _ (log "get pos: " posx posy boxw boxh selectBox)
        ]
    (js/document.body.appendChild node1)
    (.setAttribute node1 "id" id)

    (rd/render [:div
                [mask-div]
                [:div.box (attr
                           :position=absolute
                           [:width (str boxw "px")
                            :left (str posx "px")
                            :top (str posy "px")
                            ]
                           {:on-click #(log "click in box")}
                           
                           ) nodes]
                  ]
                  node1)
      
  ))
